/* 
   An object of class StatCalc can be used to compute several simple statistics
   for a set of numbers.  Numbers are entered into the dataset using
   the enter(double) method.  Methods are provided to return the following
   statistics for the set of numbers that have been entered: The number
   of items, the sum of the items, the average, and the standard deviation.
*/

public class StatCalc {

   private int count;   // Number of numbers that have been entered.
   private double sum;  // The sum of all the items that have been entered.
   private double squareSum;  // The sum of the squares of all the items.
   private double max = Double.MIN_VALUE;
   private double min = Double.MAX_VALUE;
   private int positiveCount;
   private int negativeCount;
   private double positiveSum;
   private double negativeSum;
   private int[] maxParam;
   private int[] minParam;
   
   public void enter(double num, Object param) { 
     enter(num);
     if (!(param instanceof int[])) {
       throw new RuntimeException("param in not of type int[]");
     }
	   int[] strategy = (int[])param;
	   if (max == num) {  // 在 enter(double) 中已比較變更過 max
		   maxParam = strategy;
	   } 
     if (min == num) {  // 在 enter(double) 中已比較變更過 min
		   minParam = strategy;
	   }
   }
   
   public int[] getMaxParam() {
	   return maxParam;
   }
   
   public int[] getMinParam() {
	   return minParam;
   }   
   
   public void enter(double num) {
         // Add the number to the dataset.
      count++;
      sum += num;
      squareSum += num*num;
      max = Math.max(max, num);
      min = Math.min(min, num);
      if (num > 0) {
    	  positiveCount++;
    	  positiveSum += num;
      } else {
    	  negativeCount++;
    	  negativeSum += num;
      }
   }

   public int getPositiveCount() {
	   return positiveCount;
   }
   public int getNegativeCount() {
	   return negativeCount;
   }
   public double getPositiveMean() {
	   return positiveSum / positiveCount;
   }
   public double getNegativeMean() {
	   return negativeSum / negativeCount;
   }
   public int getCount() {   
         // Return number of items that have been entered.
      return count;
   }

   public double getSum() {
         // Return the sum of all the items that have been entered.
      return sum;
   }

   public double getMean() {
         // Return average of all the items that have been entered.
         // Value is Double.NaN if count == 0.
      return sum / count;  
   }

   public double getStandardDeviation() {  
        // Return standard deviation of all the items that have been entered.
        // Value will be Double.NaN if count == 0.
      double mean = getMean();
      return Math.sqrt( squareSum/count - mean*mean );
   }
   
   public double getMax() {
	   return max;
   }
   
   public double getMin() {
	   return min;
   }

}  // end of class StatCalc